package com.oasystem.web.controller.fund;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oasystem.domain.PagingBean;
import com.oasystem.domain.fund.AppropriationApplication;
import com.oasystem.domain.user.User;
import com.oasystem.service.fund.AppropriationApplicationService;
import com.oasystem.service.system.SystemService;
import com.oasystem.utils.SystemConstants;
import com.oasystem.web.webbean.fund.AppropriationApproveWebBean;

@Controller
@RequestMapping(value = "/MainMenu/fund")
public class AppropriationController {

	@Resource(name = "systemService")
	private SystemService systemService;
	@Resource(name = "appropriationApplicationService")
	private AppropriationApplicationService appropriationApplicationService;

	@RequestMapping(value = "/appropriationApplicationPage")
	public String applicationPage(Model model) {
		model.addAttribute("mainMenu", "fund");
		model.addAttribute("subMenu", "appropriation");
		model.addAttribute("appropriationApplication",
				new AppropriationApplication());
		model.addAttribute("approverList", systemService.leaderList());
		return "fund.appropriation";
	}

	@RequestMapping(value = "/appropriationdApplicationHistory")
	public String fundApplicationHistory(Model model, String pageIndex,
			HttpSession session) {
		User user = (User) session.getAttribute(SystemConstants.USER);
		PagingBean<AppropriationApplication> pagingBean = new PagingBean<AppropriationApplication>();
		pagingBean.setCurrentPage(Integer.valueOf(pageIndex));
		// fundApplicationService.fundApplicationHistory(user.getUserID(),pagingBean);
		// TODO:
		appropriationApplicationService.appropriationApplicationHistory("1",
				pagingBean);
		model.addAttribute("pagingBean", pagingBean);
		return "fund.appropriation.history";
	}

	@RequestMapping(value = "/saveAppropriationApplication")
	public @ResponseBody
	ModelMap saveAppication(Model model,
			AppropriationApproveWebBean appropriationApproveWebBean) {
		ModelMap map = new ModelMap();
		try {
			appropriationApplicationService.insert(appropriationApproveWebBean
					.build());
		} catch (Exception e) {
			map.addAttribute("error", e.getMessage());
		}
		return map;
	}

	@RequestMapping(value = "/appropriationHistory")
	public String history(Model model, String pageIndex) {
		return "fund.appropriation.history";
	}

	@RequestMapping(value = "/appropriationApprovement")
	public String appropriationApprovement(Model model) {
		model.addAttribute("mainMenu", "fund");
		model.addAttribute("subMenu", "appropriationApprovement");
		return "appropriation.approvement";
	}
	
	@RequestMapping(value = "/appropriationApprovementList")
	public String appropriationApprovementList(Model model, String pageIndex,
			HttpSession session) {
		User user = (User) session.getAttribute(SystemConstants.USER);
		PagingBean<AppropriationApplication> pagingBean = new PagingBean<AppropriationApplication>();
		if (pageIndex == null || "".equals(pageIndex)) {
			pagingBean.setCurrentPage(1);
		} else {
			pagingBean.setCurrentPage(Integer.valueOf(pageIndex));
		}
		// TODO:
		appropriationApplicationService.appropriationApplicationListForCheck("1", pagingBean);
		model.addAttribute("pagingBean", pagingBean);
		return "appropriation.approvement.list";
	}

	@RequestMapping(value = "/approveAppropriationApplication")
	public @ResponseBody
	ModelMap approveAppropriationApplication(Model model, String appropriationApplicationId,
			HttpSession session) {
		ModelMap map = new ModelMap();
		try {
			appropriationApplicationService.approveAppropriationApplication(appropriationApplicationId);
		} catch (Exception e) {
			map.addAttribute("error", e.getMessage());
		}
		return map;
	}

	@RequestMapping(value = "/rejectAppropriationApplication")
	public @ResponseBody
	ModelMap rejectAppropriationApplication(Model model, String appropriationApplicationId,String reason,
			HttpSession session) {
		ModelMap map = new ModelMap();
		try {
			appropriationApplicationService.rejectAppropriationApplication(appropriationApplicationId,reason);
		} catch (Exception e) {
			map.addAttribute("error", e.getMessage());
		}
		return map;
	}
}
